package com.atyixiang.hosp.Controller.api;

import com.atyixiang.cmn.exception.YyghException;
import com.atyixiang.cmn.helper.HttpRequestHelper;
import com.atyixiang.cmn.result.ResultCodeEnum;
import com.atyixiang.cmn.utils.MD5;
import com.atyixiang.hosp.Service.DepartmentService;
import com.atyixiang.hosp.Service.HospService;
import com.atyixiang.hosp.Service.HospitalSetService;
import com.atyixiang.hosp.Service.ScheduleService;
import com.atyixiang.model.hosp.Department;
import com.atyixiang.model.hosp.Hospital;
import com.atyixiang.model.hosp.Schedule;
import com.atyixiang.vo.hosp.DepartmentQueryVo;
import com.atyixiang.vo.hosp.ScheduleQueryVo;
import com.atyixiang.commom.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/Hosp")
public class ApiController {

    @Autowired
    private HospService hospService;

    //医院接口
    @Autowired
    private HospitalSetService hospitalSetService;

    //科室接口
    @Autowired
    private DepartmentService departmentService;

    //排班接口
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 删除排班信息
     */

    @PostMapping("schedule/remove")
    public  Result removeSchedule(HttpServletRequest request) {
    //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院的编号 和 排班的编号
        String hoscode =(String)ObjMap.get("hoscode");
        String hosScheduleId =(String)ObjMap.get("hosScheduleId");

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //签名校验
        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //根据医院编号和排班编号进行 删除
        scheduleService.remove(hoscode,hosScheduleId);
        return  Result.ok();
    }


    /**
     * 查询排班信息
     */
    @PostMapping("schedule/list")
    public  Result findSchedule(HttpServletRequest request) {
        //**************************************************************************************
        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode =(String)ObjMap.get("hoscode");
        //获取科室的编号
        String depcode =(String)ObjMap.get("depcode");

        //获取当前页和 每页的数
        int page = StringUtils.isEmpty(ObjMap.get("page"))?1:Integer.parseInt((String)ObjMap.get("page"));
        int limit = StringUtils.isEmpty(ObjMap.get("limit"))?1:Integer.parseInt((String)ObjMap.get("limit"));

        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //**************************************************************************************

        //调用封装的实体类 实现条件分页查询
        // 把医院编号和科室的编号 设置给 ScheduleOrderVo
        ScheduleQueryVo scheduleQueryVo =new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        //调用service的方法 传入 1.当前页 2.每条记录数 3.条件
        Page<Schedule> departmentPage= scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return  Result.ok(departmentPage);
    }


    /**
     * 添加排班
     */
    @PostMapping("saveSchedule")
    public  Result saveSchedule(HttpServletRequest request){

        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //签名校验
        //获取医院的编号
        String hoscode =(String)ObjMap.get("hoscode");
        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        scheduleService.save(ObjMap);
        return Result.ok();

    }


    /**
     * 删除科室方法
     */
    @PostMapping("department/remove")
    public  Result removeDepartment(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院的编号 和 科室编号
        String hoscode =(String)ObjMap.get("hoscode");
        String depcode =(String)ObjMap.get("depcode");

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //签名校验
        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //根据医院编号和科室编号进行 删除
        departmentService.remove(hoscode,depcode);
        return  Result.ok();
    }

    /**
     * 查询科室方法
     */
    @PostMapping("department/list")
    public  Result findDepartment(HttpServletRequest request){
        //**************************************************************************************
        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院的编号
        String hoscode =(String)ObjMap.get("hoscode");

        //获取当前页和 每页的数
        int page = StringUtils.isEmpty(ObjMap.get("page"))?1:Integer.parseInt((String)ObjMap.get("page"));
        int limit = StringUtils.isEmpty(ObjMap.get("limit"))?1:Integer.parseInt((String)ObjMap.get("limit"));


        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //**************************************************************************************

        //调用封装的实体类 实现条件分页查询
        DepartmentQueryVo departmentQueryVo =new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        //调用service的方法 传入 1.当前页 2.每条记录数 3.条件
        Page<Department> departmentPage= departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return  Result.ok(departmentPage);
    }

    /**
     * 上传科室接口
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //**************************************************************************************
        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院的编号
        String hoscode =(String)ObjMap.get("hoscode");
        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");
        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //**************************************************************************************

        //调用service的方法
        departmentService.save(ObjMap);
        return  Result.ok();

    }

    /**
     * 查询医院
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital (HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode =(String)ObjMap.get("hoscode");
        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");

        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospService.getByHoscode(hoscode);
        return  Result.ok(hospital);
    }


    /**
     * 上传医院接口
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        //Map<String,String[]> 转换成 Map<String,Object>
        Map<String,Object> ObjMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名,签名进行了MD5的加密
        String hospsign = (String) ObjMap.get("sign");

        //2.根据传递过来医院编码，查询数据库，查询签名
        String hoscode =(String)ObjMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5的加密
        String signKeyMD5 = MD5.encrypt(signKey);

       //4.判断签名是否一致
        if(!hospsign.equals(signKeyMD5)){
            //使用枚举类中 设置的数据
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)ObjMap.get("logoData");
        //1.把图片编码中的空格  （replaceAll全部替换）成+号  2.转完之后在赋值给 logoData
        logoData = logoData.replaceAll(" ", "+");
        ObjMap.put("logoData", logoData);

        //调用service中的方法
        hospService.save(ObjMap);
        return Result.ok();
    }


}
